package com.dragee.modules.work.controller;

import cn.hutool.core.util.StrUtil;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.work.entity.TeamAthleteEntity;
import com.dragee.modules.work.entity.TeamEntity;
import com.dragee.modules.work.service.TeamAthleteService;
import com.dragee.modules.work.service.TeamService;
import com.dragee.utils.LoginUserUtil;
import com.dragee.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;



/**
 * 运动员
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-19 09:42:02
 */
@RestController
@RequestMapping("work/teamathlete")
@Api(tags = "work - 运动员")
public class TeamAthleteController {
    @Autowired
    private TeamAthleteService teamAthleteService;
    @Autowired
    private TeamService teamService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("运动员列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teamAthleteService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("运动员信息")
    public R info(@PathVariable("id") Long id){
		TeamAthleteEntity teamAthlete = teamAthleteService.getById(id);
        return R.ok().put("data", teamAthlete);
    }

    /**
     * 信息
     */
    @GetMapping("applyTeam")
    @ApiOperation("申请入队")
    public R applyTeam(@RequestParam("id") String id){
        TeamAthleteEntity teamAthlete = teamAthleteService.getById(id);
        teamAthlete.setApplyStatus("待通过");
        teamAthleteService.saveOrUpdate(teamAthlete);
        return R.ok();
    }

    /**
     * 信息
     */
    @GetMapping("agreeJoin")
    @ApiOperation("同意入队")
    public R agreeJoin(@RequestParam("id") String id, @RequestParam("result") String result) {
        TeamAthleteEntity teamAthlete = teamAthleteService.getById(id);
        if (result.equals("yes")) {
            teamAthlete.setApplyStatus("已加入");
        } else if (result.equals("no")) {
            teamAthlete.setApplyStatus("已拒绝");
        }
        teamAthleteService.saveOrUpdate(teamAthlete);
        return R.ok();
    }


    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改运动员")
    public R saveOrUpdate(@RequestBody TeamAthleteEntity teamAthlete){
        // 添加队伍号
        String teamId = teamAthlete.getTeamId();
        if (StrUtil.isNotBlank(teamId)) {
            TeamEntity team = teamService.getTeamByCode(teamId);
            teamAthlete.setTeamKey(team.getId().toString());
        }
        // 如果是家长则增加关联家长 且设置状态为未加入
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        if (loginUser.getRole().getRoleKey().equals("jiazhang")) {
            teamAthlete.setUserId(loginUser.getUserId().toString());
            teamAthlete.setApplyStatus("未加入");
        } else {
            teamAthlete.setApplyStatus("已加入");
        }
        teamAthleteService.saveOrUpdate(teamAthlete);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("运动员删除")
    public R delete( String id){
		teamAthleteService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("运动员批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> teamAthleteService.removeById(Long.parseLong(id)));
        return R.ok();
    }


    @GetMapping("/downloadTemplate")
    @ApiOperation(value = "下载模板", produces = "application/octet-stream")
    public void downloadTemplate(HttpServletResponse response) {
        String tempalteName = "单位导入运动员模板";
        String filepath = "static/templates/" + tempalteName + ".xlsx";
        String fileName = tempalteName + ".xlsx";
        UploadUtil.downloadTemplateFromResource(response,filepath,fileName);
    }


    @ApiOperation(value = "数据导入")
    @PostMapping("/importData")
    public R importData(@RequestParam("file") MultipartFile file) throws IOException {
        return teamAthleteService.importData(file);
    }



}
